package god530.com.simpleapp_slide.account.title;

import android.app.Activity;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.lang.ref.WeakReference;

import god530.com.simpleapp_slide.R;

/**
 * Created by root on 16-1-30.
 */
public class TitleBarUtils {
    private View iTitleView;
    private ImageView leftView;
    private FrameLayout rightView;
    private TextView mTitle;

    public TitleBarUtils(View iTitleView) {
        this.iTitleView = iTitleView;
    }

    public ImageView getLeftImageView(@IdRes int resId) {
        return (ImageView) iTitleView.findViewById(resId);
    }

    public FrameLayout getRightFrameLayoutView(@IdRes int resId) {
        return (FrameLayout) iTitleView.findViewById(resId);
    }

    public ImageView getRightImageView(@IdRes int resId) {
        ImageView iv = (ImageView) iTitleView.findViewById(resId);
        iv.setVisibility(View.VISIBLE);
        return iv;
    }

    public TextView getRightTextView(@IdRes int resId) {
        TextView tv = (TextView) iTitleView.findViewById(resId);
        tv.setVisibility(View.VISIBLE);
        return tv;
    }

    public TextView getTitleView(@IdRes int resId) {
        return (TextView) iTitleView.findViewById(resId);
    }

    public void setLeftView(@IdRes int resId, @DrawableRes int drawId) {
        getLeftImageView(resId).setImageResource(drawId);
    }

    public void setLeftView(@DrawableRes int drawId) {
        getLeftImageView(R.id.title_left).setImageResource(drawId);
    }

    public void setRightImageView(@IdRes int resId, @DrawableRes int drawId) {
        getRightImageView(resId).setImageResource(drawId);
    }

    public void setRightImageView(@DrawableRes int drawId) {
        setRightImageView(R.id.title_right_image, drawId);
    }

//    public void setRightView(@IdRes int resId, @DrawableRes int drawId) {
//        getRightImageView(resId).setImageResource(drawId);
//    }
//
//    public void setRightView(@DrawableRes int drawId) {
//        getRightImageView(R.id.title_right).setImageResource(drawId);
//    }

    public void setTitleView(@IdRes int resId, @NonNull CharSequence title) {
        getTitleView(resId).setText(title);
    }

    public void setTitleView(@NonNull CharSequence title) {
        getTitleView(R.id.title_title).setText(title);
    }

    /**
     * 设置点击事件
     * @param resId
     * @param activity
     */
    public void setLeftViewClickListener(@IdRes int resId, final Activity activity){
        final WeakReference<Activity> activityWeakReference = new WeakReference<>(activity);
        if(leftView == null) {
            leftView = getLeftImageView(resId);
        }
        leftView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity activity1 = activityWeakReference.get();
                if (activity1 != null) {
                    activity1.finish();
                }
            }
        });
    }

    public void setLeftViewClickListener(Activity activity) {
        setLeftViewClickListener(R.id.title_left, activity);
    }

    public void setRightViewClickListener(@IdRes int resId, @NonNull View.OnClickListener listener) {
        if(rightView == null) {
            rightView = getRightFrameLayoutView(resId);
        }
        rightView.setOnClickListener(listener);
    }

    public void setRightViewClickListener(@NonNull View.OnClickListener listener) {
        setRightViewClickListener(R.id.title_right, listener);
    }

}
